/**
 * User: Allen
 * Date: 12-4-27
 */
window.onload=function(){
    new MaskIt('regBtn','cloBtn','mask','dialog');
    new ToTop($('toTop'),720,500);
    new ReplyIt(getByClass('text'),50,50,'提问');
    new ReplyIt(getByClass('replyMes'),24,50,'回复');
    var model=$('model');
    var msgContent=$('content');
    var fm=$('submit1');
    model.style.display='none';
    fm.onsubmit = function(evt){
        if (evt) {
            evt.preventDefault();
        }
        else {
            window.event.returnValue = false;
        };
//**************************************
      //将用户输入异步提交到服务器
      	//获取用户输入
      	//var title=document.forms[0].title.value;
      	var userId=document.forms[0].userId.value;
      	var content=document.forms[0].content.value;
      	//alert(content);
      	//创建XMLHttpRequest对象
      	var xmlhttp;
     	try{
      		xmlhttp=new XMLHttpRequest();
      	}catch(e){
      		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      	}
      	//创建请求结果处理程序
      	xmlhttp.onreadystatechange=function(){
      		if (4==xmlhttp.readyState){
      			if (200==xmlhttp.status){
      				//var date=xmlhttp.responseText;
      			     if (content!='提问') {
      		            var newSection =model.cloneNode(true),
      		                msgDateSpan = getByClass('timeSpan', newSection)[0],
      		                msgContentDiv = getByClass('contentSpan', newSection)[0];
      		            newSection.style.display='block';     		            
      		            msgDateSpan.innerHTML ='时间：'+ date('Y-m-d H:i:s');
      		            msgContentDiv.innerHTML = htmlEncode(content);
      		            model.parentNode.insertBefore(newSection,model.nextSibling);    		            
      		        }
      			     document.forms[0].content.value='提问';
      			}else{
      				alert("error");
      			}
      		}  
      	}
      	//打开连接，true表示异步提交
      	xmlhttp.open("post", "/HongZhu/AddComment", true);
      	//当方法为post时需要如下设置http头
      	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
      	//发送数据
      	xmlhttp.send("userId="+userId+"&content="+content);
  	}
    
var replyForms=getByClass('replyForm');
var mode2=$('mode2');
for(var i=0;i<replyForms.length;i++){
	var contents=getByClass('replyMes',replyForms[i]);
	replyForms[i].onsubmit=function(evt){
		if (evt) {
            evt.preventDefault();
        }
        else {
            window.event.returnValue = false;
        };
      	var userId2=this.userId.value;
      	var targetId=this.targetId.value;
      	var content2=this.content.value;
      	//alert(userId2);
      	var xmlhttp2;
     	try{
      		xmlhttp2=new XMLHttpRequest();
      	}catch(e){
      		xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
      	}
      	var _this=this;
      	xmlhttp2.onreadystatechange=function(){
      		if (4==xmlhttp2.readyState){
      			if (200==xmlhttp2.status){
      			     if (content2!='回复') {
      		            var newDiv =mode2.cloneNode(true),
      		                msgDateSpan = getByClass('timeSpan', newDiv)[0],
      		                msgContentDiv = getByClass('contentSpan', newDiv)[0];
      		            newDiv.style.display='block';
      		            msgDateSpan.innerHTML ='时间：'+ date('Y-m-d H:i:s');
      		            msgContentDiv.innerHTML = htmlEncode(content2);
      		           
      		            _this.parentNode.insertBefore(newDiv,_this);    		            
      		        }
      			     _this.content.value='回复';
      			}else{
      				alert("error");
      			}
      		}
      	}
    	//打开连接，true表示异步提交
      	xmlhttp2.open("post", "/HongZhu/AddReply", true);
      	//当方法为post时需要如下设置http头
      	xmlhttp2.setRequestHeader('Content-type','application/x-www-form-urlencoded');
      	//发送数据
      	xmlhttp2.send("userId="+userId2+"&content="+content2+"&targetId="+targetId);
      	}
	}
};
function MaskIt(regBtn,cloBtn,mask,dialog){
    this.regBtn=$(regBtn);
    this.cloBtn=$(cloBtn);
    this.mask=$(mask);
    this.dialog=$(dialog);
    var _this=this;
    if(this.regBtn){
    	this.regBtn.onclick=function(){
       		_this.show();
    	}
    }
    if(this.cloBtn){
    	this.cloBtn.onclick=function(){
        	_this.hide();
    	}
    }
    
}
MaskIt.prototype={
    show:function(){
        this.mask.style.display='block';
        this.dialog.style.display='block';
        document.oncontextmenu=function(){
            return false;
        }
    },
    hide:function(){
        this.mask.style.display='none';
        this.dialog.style.display='none';
        document.oncontextmenu=function(){
            return true;
        }
    }
}
function ReplyIt(replyText,rHeight,height,txt){
    this.replyText=replyText;
    this.rHeight=rHeight;
    this.height=height || 50;
    this.txt= txt || '回复给老师';
    var _this=this;
    for(var i=0;i<this.replyText.length;i++){
        this.replyText[i].value=this.txt;
        this.replyText[i].onfocus=function(){
            if(this.value==_this.txt){
                this.value='';
            }
           this.style.height=_this.height+'px';
          nextNode(this).style.display = 'block';
           nextNode(this).onmouseover=function(){
                this.style.color='#F9931C';
            }
            nextNode(this).onmouseout=function(){
                this.style.color='#C7C7C7';
            }
        }
        this.replyText[i].onblur=function(){
            if(!this.value){
                this.value=_this.txt;
                this.style.height=_this.rHeight+'px';
                nextNode(this).style.display='none';
            }
        }
    }
}
function ToTop(btn,constant,topSpace){
    this.btn=btn;
    this.topSpace=topSpace || 0;
    this.constant=constant;
    this.btn.onclick=function(){
        window.scrollTo(0,0);
    }
    var _this=this;
    window.onresize=function(){
        _this.getWinSize();
    }
    window.onscroll=function(){
        _this.scroll();
    }
    this.getWinSize();
    this.scroll();
}
ToTop.prototype={
    getWinSize:function(){
        var width=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
        this.btn.style.right=(width-this.constant)/2 + "px";
    },
    scroll:function(){
        if(document.documentElement.scrollTop>this.topSpace){
            this.btn.style.display='block';
        } else
        if(document.body.scrollTop>this.topSpace){
            this.btn.style.display='block';
        } else{
            this.btn.style.display='none';
        }
    }
}
